Spot-On
libspoton.h
1 /*
2 ** Copyright (c) 2011 - 10^10^10, Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 ** 1. Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** 3. The name of the author may not be used to endorse or promote products
14 ** derived from Spot-On without specific prior written permission.
15 **
16 ** SPOT-ON IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 ** SPOT-ON, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef LIBSPOTON_H
29 #define LIBSPOTON_H
30 
31 #define LIBSPOTON_VERSION 0x000106
32 #define LIBSPOTON_VERSION_STR "0.1.6"
33 
34 #ifdef LIBSPOTON_OS_WINDOWS
35 #include "errno.h"
36 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
37 #include "gcrypt.h"
38 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
39 #include "pthread.h"
40 #include "sqlite3.h"
41 #else
42 #include <arpa/inet.h>
43 #include <errno.h>
44 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
45 #include <gcrypt.h>
46 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
47 #include <pthread.h>
48 #include <sqlite3.h>
49 #endif
50 #include <stdbool.h>
51 #include <stdint.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 
56 #ifdef __cplusplus
57 extern "C"
58 {
59 #endif
60  typedef enum
61  {
62  LIBSPOTON_ERROR_NONE = 0,
63  LIBSPOTON_ERROR_CALLOC,
64  LIBSPOTON_ERROR_GCRY_CALLOC,
65  LIBSPOTON_ERROR_GCRY_CALLOC_SECURE,
66  LIBSPOTON_ERROR_GCRY_CHECK_VERSION,
67  LIBSPOTON_ERROR_GCRY_CIPHER_ENCRYPT,
68  LIBSPOTON_ERROR_GCRY_CIPHER_GET_ALGO_BLKLEN,
69  LIBSPOTON_ERROR_GCRY_CIPHER_GET_ALGO_KEYLEN,
70  LIBSPOTON_ERROR_GCRY_CIPHER_MAP_NAME,
71  LIBSPOTON_ERROR_GCRY_CIPHER_OPEN,
72  LIBSPOTON_ERROR_GCRY_CIPHER_SETIV,
73  LIBSPOTON_ERROR_GCRY_CIPHER_SETKEY,
74  LIBSPOTON_ERROR_GCRY_CONTROL,
75  LIBSPOTON_ERROR_GCRY_KDF_DERIVE,
76  LIBSPOTON_ERROR_GCRY_MD_MAP_NAME,
77  LIBSPOTON_ERROR_INVALID_LENGTH,
78  LIBSPOTON_ERROR_INVALID_PARAMETER,
79  LIBSPOTON_ERROR_KERNEL_PROCESS_ALREADY_REGISTERED,
80  LIBSPOTON_ERROR_NOT_CONNECTED_TO_SQLITE_DATABASE,
81  LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE,
82  LIBSPOTON_ERROR_NUMERIC_ERROR,
83  LIBSPOTON_ERROR_SQLITE_BIND_BLOB_CONTENT,
84  LIBSPOTON_ERROR_SQLITE_BIND_BLOB_DESCRIPTION,
85  LIBSPOTON_ERROR_SQLITE_BIND_BLOB_TITLE,
86  LIBSPOTON_ERROR_SQLITE_BIND_BLOB_URL,
87  LIBSPOTON_ERROR_SQLITE_BIND_INT,
88  LIBSPOTON_ERROR_SQLITE_BIND_INT64,
89  LIBSPOTON_ERROR_SQLITE_BIND_INT_ENCRYPT,
90  LIBSPOTON_ERROR_SQLITE_CREATE_KERNEL_REGISTRATION_TABLE,
91  LIBSPOTON_ERROR_SQLITE_CREATE_KERNEL_REGISTRATION_TRIGGER,
92  LIBSPOTON_ERROR_SQLITE_CREATE_URLS_TABLE,
93  LIBSPOTON_ERROR_SQLITE_DATABASE_LOCKED,
94  LIBSPOTON_ERROR_SQLITE_OPEN_V2,
95  LIBSPOTON_ERROR_SQLITE_PREPARE_V2,
96  LIBSPOTON_ERROR_SQLITE_STEP
97  } libspoton_error_code_t;
98 
100  {
101  char *m_key;
102  int m_cipherAlgorithm;
103  size_t m_keyLength;
104  sqlite3 *m_sqliteHandle;
105  };
106 
107  typedef libspoton_error_code_t libspoton_error_t;
109 
110  static bool libspoton_gcryctl_set_thread_cbs_set = false;
111  static const char *libspoton_error_strings[] =
112  {
113  "LIBSPOTON_ERROR_NONE",
114  "LIBSPOTON_ERROR_CALLOC",
115  "LIBSPOTON_ERROR_CALLOC_SECURE",
116  "LIBSPOTON_ERROR_GCRY_CALLOC",
117  "LIBSPOTON_ERROR_GCRY_CHECK_VERSION",
118  "LIBSPOTON_ERROR_GCRY_CIPHER_ENCRYPT",
119  "LIBSPOTON_ERROR_GCRY_CIPHER_GET_ALGO_BLKLEN",
120  "LIBSPOTON_ERROR_GCRY_CIPHER_GET_ALGO_KEYLEN",
121  "LIBSPOTON_ERROR_GCRY_CIPHER_MAP_NAME",
122  "LIBSPOTON_ERROR_GCRY_CIPHER_OPEN",
123  "LIBSPOTON_ERROR_GCRY_CIPHER_SETIV",
124  "LIBSPOTON_ERROR_GCRY_CIPHER_SETKEY",
125  "LIBSPOTON_ERROR_GCRY_CONTROL",
126  "LIBSPOTON_ERROR_GCRY_KDF_DERIVE",
127  "LIBSPOTON_ERROR_GCRY_MD_MAP_NAME",
128  "LIBSPOTON_ERROR_INVALID_LENGTH",
129  "LIBSPOTON_ERROR_INVALID_PARAMETER",
130  "LIBSPOTON_ERROR_KERNEL_PROCESS_ALREADY_REGISTERED",
131  "LIBSPOTON_ERROR_NOT_CONNECTED_TO_SQLITE_DATABASE",
132  "LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE",
133  "LIBSPOTON_ERROR_NUMERIC_ERROR",
134  "LIBSPOTON_ERROR_SQLITE_BIND_BLOB_CONTENT",
135  "LIBSPOTON_ERROR_SQLITE_BIND_BLOB_DESCRIPTION",
136  "LIBSPOTON_ERROR_SQLITE_BIND_BLOB_TITLE",
137  "LIBSPOTON_ERROR_SQLITE_BIND_BLOB_URL",
138  "LIBSPOTON_ERROR_SQLITE_BIND_INT",
139  "LIBSPOTON_ERROR_SQLITE_BIND_INT64",
140  "LIBSPOTON_ERROR_SQLITE_BIND_INT_ENCRYPT",
141  "LIBSPOTON_ERROR_SQLITE_CREATE_KERNEL_REGISTRATION_TABLE",
142  "LIBSPOTON_ERROR_SQLITE_CREATE_KERNEL_REGISTRATION_TRIGGER",
143  "LIBSPOTON_ERROR_SQLITE_CREATE_URLS_TABLE",
144  "LIBSPOTON_ERROR_SQLITE_DATABASE_LOCKED",
145  "LIBSPOTON_ERROR_SQLITE_OPEN_V2",
146  "LIBSPOTON_ERROR_SQLITE_PREPARE_V2",
147  "LIBSPOTON_ERROR_SQLITE_STEP"
148  };
149  static libspoton_error_t libspoton_error_maximum_error =
150  LIBSPOTON_ERROR_SQLITE_STEP;
151  static pthread_mutex_t sqlite_mutex = PTHREAD_MUTEX_INITIALIZER;
152 
153 #if !defined(GCRYPT_VERSION_NUMBER) || GCRYPT_VERSION_NUMBER < 0x010600
154  GCRY_THREAD_OPTION_PTHREAD_IMPL;
155 #endif
156 
157  static inline libspoton_error_t initialize_libgcrypt
158  (const int secure_memory_pool_size)
159  {
160  /*
161  ** Initialize the gcrypt library if it has not yet been initialized.
162  */
163 
164  libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
165 
166  if(!libspoton_gcryctl_set_thread_cbs_set)
167  {
168  gcry_error_t err = 0;
169 
170 #if !defined(GCRYPT_VERSION_NUMBER) || GCRYPT_VERSION_NUMBER < 0x010600
171  err = gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread, 0);
172 #endif
173 
174  if(err == 0)
175  libspoton_gcryctl_set_thread_cbs_set = true;
176  else if(stderr)
177  fprintf(stderr, "libspoton::initialize_libgcrypt(): "
178  "error initializing threads. Proceeding.\n");
179  }
180 
181  if(!gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
182  {
183  gcry_control(GCRYCTL_ENABLE_M_GUARD);
184 
185  if(!gcry_check_version(GCRYPT_VERSION))
186  rerr = LIBSPOTON_ERROR_GCRY_CHECK_VERSION;
187  else if(secure_memory_pool_size == 0)
188  {
189  gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
190  gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
191  }
192  else
193  {
194  gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN);
195 #ifdef LIBSPOTON_IGNORE_GCRY_CONTROL_GCRYCTL_INIT_SECMEM_RETURN_VALUE
196  gcry_control(GCRYCTL_INIT_SECMEM, abs(secure_memory_pool_size), 0);
197 #else
198  if(gcry_control(GCRYCTL_INIT_SECMEM,
199  abs(secure_memory_pool_size), 0) != 0)
200  rerr = LIBSPOTON_ERROR_GCRY_CONTROL;
201 #endif
202  gcry_control(GCRYCTL_RESUME_SECMEM_WARN);
203  gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
204  }
205  }
206 
207  return rerr;
208  }
209 
210  /*
211  ** Is a kernel process registered?
212  */
213 
214  bool libspoton_is_kernel_registered(libspoton_handle_t *libspotonHandle,
215  libspoton_error_t *error);
216 
217  /*
218  ** Return a user-friendly string representation of error.
219  */
220 
221  const char *libspoton_strerror(const libspoton_error_t error);
222 
223  libspoton_error_t libspoton_create_urls_table
224  (libspoton_handle_t *libspotonHandle);
225 
226  /*
227  ** Delete URLs.
228  */
229 
230  libspoton_error_t libspoton_delete_urls(libspoton_handle_t *libspotonHandle,
231  const int encrypted);
232 
233  /*
234  ** Deregister the kernel process.
235  */
236 
237  libspoton_error_t libspoton_deregister_kernel
238  (const pid_t pid, libspoton_handle_t *libspotonHandle);
239 
240  /*
241  ** Create shared.db. Initialize containers.
242  */
243 
244  libspoton_error_t libspoton_init_a(const char *databasePath,
245  const char *cipherType,
246  const char *key,
247  const size_t keyLength,
248  libspoton_handle_t *libspotonHandle,
249  const int secure_memory_pool_size);
250  libspoton_error_t libspoton_init_b(const char *databasePath,
251  const char *cipherType,
252  const char *hashType,
253  const char *passphrase,
254  const char *salt,
255  const size_t passphraseLength,
256  const size_t saltLength,
257  const unsigned long iterationCount,
258  libspoton_handle_t *libspotonHandle,
259  const int secure_memory_pool_size);
260 
261  /*
262  ** Register a kernel process.
263  */
264 
265  libspoton_error_t libspoton_register_kernel
266  (const pid_t pid,
267  const bool forceRegistration,
268  libspoton_handle_t *libspotonHandle);
269 
270  /*
271  ** Encode the content, description, title, and url via cipherType and
272  ** place the encoded values into the urls table. The url field is required
273  ** while content, description, and title may be 0.
274  ** Please note that the passphrase must agree with the passphrase
275  ** understood by Spot-On.
276  */
277 
278  libspoton_error_t libspoton_save_url(const char *url,
279  const size_t urlSize,
280  const char *title,
281  const size_t titleSize,
282  const char *description,
283  const size_t descriptionSize,
284  const char *content,
285  const size_t contentSize,
286  libspoton_handle_t *libspotonHandle);
287 
288  /*
289  ** Retrieve the registered kernel's PID.
290  */
291 
292  pid_t libspoton_registered_kernel_pid(libspoton_handle_t *libspotonHandle,
293  libspoton_error_t *error);
294 
295  /*
296  ** Release resources and reset important containers to zero.
297  */
298 
299  void libspoton_close(libspoton_handle_t *libspotonHandle);
300  void libspoton_enable_sqlite_cache(void);
301 
302  inline bool libspoton_is_kernel_registered
303  (libspoton_handle_t *libspotonHandle, libspoton_error_t *error)
304  {
305  return libspoton_registered_kernel_pid(libspotonHandle, error) > 0;
306  }
307 
308  inline const char *libspoton_strerror(const libspoton_error_t error)
309  {
310  if(error > libspoton_error_maximum_error)
311  return "";
312  else
313  return libspoton_error_strings[error];
314  }
315 
316  inline libspoton_error_t libspoton_create_urls_table
317  (libspoton_handle_t *libspotonHandle)
318  {
319  int rv = 0;
320  libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
321 
322  if(!libspotonHandle)
323  {
324  rerr = LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE;
325  goto error_label;
326  }
327  else if(!libspotonHandle->m_sqliteHandle)
328  {
329  rerr = LIBSPOTON_ERROR_NOT_CONNECTED_TO_SQLITE_DATABASE;
330  goto error_label;
331  }
332 
333  pthread_mutex_lock(&sqlite_mutex);
334  rv = sqlite3_exec(libspotonHandle->m_sqliteHandle,
335  "CREATE TABLE IF NOT EXISTS urls ("
336  "content BLOB, "
337  "description BLOB, "
338  "encrypted INTEGER NOT NULL DEFAULT 0, "
339  "title BLOB, "
340  "url BLOB PRIMARY KEY NOT NULL)",
341  0,
342  0,
343  0);
344  pthread_mutex_unlock(&sqlite_mutex);
345 
346  if(rv != SQLITE_OK)
347  {
348  rerr = LIBSPOTON_ERROR_SQLITE_CREATE_URLS_TABLE;
349  goto error_label;
350  }
351 
352  error_label:
353  return rerr;
354  }
355 
356  inline libspoton_error_t libspoton_delete_urls
357  (libspoton_handle_t *libspotonHandle, const int encrypted)
358  {
359  const char *sql = "DELETE FROM urls WHERE encrypted = ?";
360  int rv = 0;
361  libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
362  sqlite3_stmt *stmt = 0;
363 
364  if(!libspotonHandle)
365  {
366  rerr = LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE;
367  goto error_label;
368  }
369  else if(!libspotonHandle->m_sqliteHandle)
370  {
371  rerr = LIBSPOTON_ERROR_NOT_CONNECTED_TO_SQLITE_DATABASE;
372  goto error_label;
373  }
374 
375  pthread_mutex_lock(&sqlite_mutex);
376  rv = sqlite3_prepare_v2(libspotonHandle->m_sqliteHandle,
377  sql,
378  (int) strlen(sql),
379  &stmt,
380  0);
381  pthread_mutex_unlock(&sqlite_mutex);
382 
383  if(rv != SQLITE_OK)
384  {
385  rerr = LIBSPOTON_ERROR_SQLITE_PREPARE_V2;
386  goto error_label;
387  }
388 
389  if(sqlite3_bind_int(stmt, 1, encrypted) != SQLITE_OK)
390  {
391  rerr = LIBSPOTON_ERROR_SQLITE_BIND_INT;
392  goto error_label;
393  }
394 
395  rv = sqlite3_step(stmt);
396 
397  if(!(rv == SQLITE_OK || rv == SQLITE_DONE))
398  {
399  rerr = LIBSPOTON_ERROR_SQLITE_STEP;
400  goto error_label;
401  }
402 
403  error_label:
404  sqlite3_finalize(stmt);
405  return rerr;
406  }
407 
408  inline libspoton_error_t libspoton_deregister_kernel
409  (const pid_t pid, libspoton_handle_t *libspotonHandle)
410  {
411  const char *sql = "DELETE FROM kernel_registration WHERE pid = ?";
412  int rv = 0;
413  libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
414  sqlite3_stmt *stmt = 0;
415 
416  if(!libspotonHandle)
417  {
418  rerr = LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE;
419  goto error_label;
420  }
421  else if(!libspotonHandle->m_sqliteHandle)
422  {
423  rerr = LIBSPOTON_ERROR_NOT_CONNECTED_TO_SQLITE_DATABASE;
424  goto error_label;
425  }
426 
427  pthread_mutex_lock(&sqlite_mutex);
428  rv = sqlite3_prepare_v2(libspotonHandle->m_sqliteHandle,
429  sql,
430  (int) strlen(sql),
431  &stmt,
432  0);
433  pthread_mutex_unlock(&sqlite_mutex);
434 
435  if(rv != SQLITE_OK)
436  {
437  rerr = LIBSPOTON_ERROR_SQLITE_PREPARE_V2;
438  goto error_label;
439  }
440 
441  if(sqlite3_bind_int64(stmt, 1, (sqlite3_int64) pid) != SQLITE_OK)
442  {
443  rerr = LIBSPOTON_ERROR_SQLITE_BIND_INT64;
444  goto error_label;
445  }
446 
447  rv = sqlite3_step(stmt);
448 
449  if(!(rv == SQLITE_OK || rv == SQLITE_DONE))
450  {
451  rerr = LIBSPOTON_ERROR_SQLITE_STEP;
452  goto error_label;
453  }
454 
455  error_label:
456  sqlite3_finalize(stmt);
457  return rerr;
458  }
459 
460  inline libspoton_error_t libspoton_init_a(const char *databasePath,
461  const char *cipherType,
462  const char *key,
463  const size_t keyLength,
464  libspoton_handle_t *libspotonHandle,
465  const int secure_memory_pool_size)
466  {
467  int rv = 0;
468  libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
469 
470  if(!libspotonHandle)
471  {
472  rerr = LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE;
473  goto error_label;
474  }
475 
476  libspotonHandle->m_cipherAlgorithm = 0;
477  libspotonHandle->m_key = 0;
478  libspotonHandle->m_keyLength = 0;
479  libspotonHandle->m_sqliteHandle = 0;
480 
481  /*
482  ** Initialize libgcrypt.
483  */
484 
485  if((rerr = initialize_libgcrypt(secure_memory_pool_size)) !=
486  LIBSPOTON_ERROR_NONE)
487  goto error_label;
488 
489  if(key && keyLength > 0)
490  {
491  if((libspotonHandle->m_cipherAlgorithm =
492  gcry_cipher_map_name(cipherType)) == 0)
493  {
494  rerr = LIBSPOTON_ERROR_GCRY_CIPHER_MAP_NAME;
495  goto error_label;
496  }
497 
498  libspotonHandle->m_keyLength = keyLength;
499 
500  if(!(libspotonHandle->m_key = (char *)
501  gcry_calloc_secure(libspotonHandle->m_keyLength, sizeof(char))))
502  {
503  rerr = LIBSPOTON_ERROR_GCRY_CALLOC_SECURE;
504  goto error_label;
505  }
506  else
507  memcpy(libspotonHandle->m_key, key, keyLength);
508  }
509 
510  /*
511  ** Create the shared.db database.
512  */
513 
514  pthread_mutex_lock(&sqlite_mutex);
515  rv = sqlite3_open_v2(databasePath,
516  &libspotonHandle->m_sqliteHandle,
517  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
518  0);
519  pthread_mutex_unlock(&sqlite_mutex);
520 
521  if(rv != SQLITE_OK)
522  {
523  rerr = LIBSPOTON_ERROR_SQLITE_OPEN_V2;
524  goto error_label;
525  }
526 
527  error_label:
528 
529  if(rerr != LIBSPOTON_ERROR_NONE)
530  libspoton_close(libspotonHandle);
531 
532  return rerr;
533  }
534 
535  inline libspoton_error_t libspoton_init_b(const char *databasePath,
536  const char *cipherType,
537  const char *hashType,
538  const char *passphrase,
539  const char *salt,
540  const size_t passphraseLength,
541  const size_t saltLength,
542  const unsigned long iterationCount,
543  libspoton_handle_t *libspotonHandle,
544  const int secure_memory_pool_size)
545  {
546  int hashAlgorithm = 0;
547  int rv = 0;
548  libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
549 
550  if(!libspotonHandle)
551  {
552  rerr = LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE;
553  goto error_label;
554  }
555 
556  libspotonHandle->m_cipherAlgorithm = 0;
557  libspotonHandle->m_key = 0;
558  libspotonHandle->m_keyLength = 0;
559  libspotonHandle->m_sqliteHandle = 0;
560 
561  /*
562  ** Initialize libgcrypt.
563  */
564 
565  if((rerr =
566  initialize_libgcrypt(secure_memory_pool_size)) != LIBSPOTON_ERROR_NONE)
567  goto error_label;
568 
569  if(cipherType && hashType && iterationCount > 0 &&
570  passphrase && salt && passphraseLength > 0 && saltLength > 0)
571  {
572  if((libspotonHandle->m_cipherAlgorithm =
573  gcry_cipher_map_name(cipherType)) == 0)
574  {
575  rerr = LIBSPOTON_ERROR_GCRY_CIPHER_MAP_NAME;
576  goto error_label;
577  }
578 
579  if((hashAlgorithm = gcry_md_map_name(hashType)) == 0)
580  {
581  rerr = LIBSPOTON_ERROR_GCRY_MD_MAP_NAME;
582  goto error_label;
583  }
584 
585  if((libspotonHandle->m_keyLength =
586  gcry_cipher_get_algo_keylen(libspotonHandle->
587  m_cipherAlgorithm)) == 0)
588  {
589  rerr = LIBSPOTON_ERROR_GCRY_CIPHER_GET_ALGO_KEYLEN;
590  goto error_label;
591  }
592 
593  if(!(libspotonHandle->m_key = (char *)
594  gcry_calloc_secure(libspotonHandle->m_keyLength, sizeof(char))))
595  {
596  rerr = LIBSPOTON_ERROR_GCRY_CALLOC_SECURE;
597  goto error_label;
598  }
599 
600  gcry_fast_random_poll();
601 
602  if(gcry_kdf_derive((const void *) passphrase,
603  passphraseLength,
604  GCRY_KDF_PBKDF2,
605  hashAlgorithm,
606  (const void *) salt,
607  saltLength,
608  iterationCount,
609  libspotonHandle->m_keyLength,
610  (void *) libspotonHandle->m_key) != 0)
611  {
612  rerr = LIBSPOTON_ERROR_GCRY_KDF_DERIVE;
613  goto error_label;
614  }
615  }
616 
617  /*
618  ** Create the shared.db database.
619  */
620 
621  pthread_mutex_lock(&sqlite_mutex);
622  rv = sqlite3_open_v2(databasePath,
623  &libspotonHandle->m_sqliteHandle,
624  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
625  0);
626  pthread_mutex_unlock(&sqlite_mutex);
627 
628  if(rv != SQLITE_OK)
629  {
630  rerr = LIBSPOTON_ERROR_SQLITE_OPEN_V2;
631  goto error_label;
632  }
633 
634  error_label:
635 
636  if(rerr != LIBSPOTON_ERROR_NONE)
637  libspoton_close(libspotonHandle);
638 
639  return rerr;
640  }
641 
642  inline libspoton_error_t libspoton_register_kernel
643  (const pid_t pid,
644  const bool forceRegistration,
645  libspoton_handle_t *libspotonHandle)
646  {
647  const char *sql = "INSERT OR REPLACE INTO kernel_registration (pid) "
648  "VALUES (?)";
649  int rv = 0;
650  libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
651  sqlite3_stmt *stmt = 0;
652 
653  if(!libspotonHandle)
654  {
655  rerr = LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE;
656  goto error_label;
657  }
658  else if(!libspotonHandle->m_sqliteHandle)
659  {
660  rerr = LIBSPOTON_ERROR_NOT_CONNECTED_TO_SQLITE_DATABASE;
661  goto error_label;
662  }
663 
664  pthread_mutex_lock(&sqlite_mutex);
665  rv = sqlite3_exec(libspotonHandle->m_sqliteHandle,
666  "CREATE TABLE IF NOT EXISTS kernel_registration ("
667  "pid INTEGER PRIMARY KEY NOT NULL)",
668  0,
669  0,
670  0);
671  pthread_mutex_unlock(&sqlite_mutex);
672 
673  if(rv != SQLITE_OK)
674  {
675  rerr = LIBSPOTON_ERROR_SQLITE_CREATE_KERNEL_REGISTRATION_TABLE;
676  goto error_label;
677  }
678 
679  /*
680  ** The kernel_registration table must contain only one entry.
681  */
682 
683  pthread_mutex_lock(&sqlite_mutex);
684  rv = sqlite3_exec
685  (libspotonHandle->m_sqliteHandle,
686  "CREATE TRIGGER IF NOT EXISTS kernel_registration_trigger "
687  "BEFORE INSERT ON kernel_registration "
688  "BEGIN "
689  "DELETE FROM kernel_registration; "
690  "END",
691  0,
692  0,
693  0);
694  pthread_mutex_unlock(&sqlite_mutex);
695 
696  if(rv != SQLITE_OK)
697  {
698  rerr = LIBSPOTON_ERROR_SQLITE_CREATE_KERNEL_REGISTRATION_TRIGGER;
699  goto error_label;
700  }
701 
702  if(libspoton_registered_kernel_pid(libspotonHandle, &rerr) > 0)
703  if(!forceRegistration)
704  {
705  rerr = LIBSPOTON_ERROR_KERNEL_PROCESS_ALREADY_REGISTERED;
706  goto error_label;
707  }
708 
709  if(rerr != LIBSPOTON_ERROR_NONE)
710  goto error_label;
711 
712  pthread_mutex_lock(&sqlite_mutex);
713  rv = sqlite3_prepare_v2(libspotonHandle->m_sqliteHandle,
714  sql,
715  (int) strlen(sql),
716  &stmt,
717  0);
718  pthread_mutex_unlock(&sqlite_mutex);
719 
720  if(rv != SQLITE_OK)
721  {
722  rerr = LIBSPOTON_ERROR_SQLITE_PREPARE_V2;
723  goto error_label;
724  }
725 
726  if(sqlite3_bind_int64(stmt, 1, (sqlite3_int64) pid) != SQLITE_OK)
727  {
728  rerr = LIBSPOTON_ERROR_SQLITE_BIND_INT64;
729  goto error_label;
730  }
731 
732  rv = sqlite3_step(stmt);
733 
734  if(!(rv == SQLITE_OK || rv == SQLITE_DONE))
735  {
736  rerr = LIBSPOTON_ERROR_SQLITE_STEP;
737  goto error_label;
738  }
739 
740  error_label:
741  sqlite3_finalize(stmt);
742  return rerr;
743  }
744 
745  inline libspoton_error_t libspoton_save_url
746  (const char *url,
747  const size_t urlSize,
748  const char *title,
749  const size_t titleSize,
750  const char *description,
751  const size_t descriptionSize,
752  const char *content,
753  const size_t contentSize,
754  libspoton_handle_t *libspotonHandle)
755  {
756  bool encrypt = true;
757  char *encodedBuffer = 0;
758  char *encodedBufferAndIV = 0;
759  char *iv = 0;
760  char lengthArray[4]; /* We assume that integers are four bytes wide. */
761  const char *buffer = "";
762  const char *sql = "INSERT OR REPLACE INTO urls (url, title, "
763  "description, content, encrypted) VALUES (?, ?, ?, ?, ?)";
764  int FIELDS_COUNT = 4; /* Ignore the encrypted field. */
765  gcry_cipher_hd_t cipherCtx = 0;
766  int i = 0;
767  int rv = 0;
768  libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
769  size_t blockLength = 0;
770  size_t encodedBufferAndIVLength = 0;
771  size_t encodedBufferLength = 0;
772  size_t length = 0;
773  size_t sizeofchar = sizeof(char);
774  sqlite3_stmt *stmt = 0;
775 
776  if(!libspotonHandle)
777  {
778  rerr = LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE;
779  goto error_label;
780  }
781  else if(!libspotonHandle->m_sqliteHandle)
782  {
783  rerr = LIBSPOTON_ERROR_NOT_CONNECTED_TO_SQLITE_DATABASE;
784  goto error_label;
785  }
786 
787  if(!libspotonHandle->m_cipherAlgorithm ||
788  !libspotonHandle->m_key || !libspotonHandle->m_keyLength)
789  encrypt = false;
790 
791  if(encrypt)
792  if(gcry_cipher_open(&cipherCtx, libspotonHandle->m_cipherAlgorithm,
793  GCRY_CIPHER_MODE_CBC,
794  GCRY_CIPHER_CBC_CTS |
795  GCRY_CIPHER_SECURE) != 0 || !cipherCtx)
796  {
797  rerr = LIBSPOTON_ERROR_GCRY_CIPHER_OPEN;
798  goto error_label;
799  }
800 
801  if(encrypt)
802  {
803  if(gcry_cipher_setkey(cipherCtx,
804  libspotonHandle->m_key,
805  libspotonHandle->m_keyLength) != 0)
806  {
807  rerr = LIBSPOTON_ERROR_GCRY_CIPHER_SETKEY;
808  goto error_label;
809  }
810  }
811 
812  if(encrypt)
813  if((blockLength = gcry_cipher_get_algo_blklen(libspotonHandle->
814  m_cipherAlgorithm)) == 0)
815  {
816  rerr = LIBSPOTON_ERROR_GCRY_CIPHER_GET_ALGO_BLKLEN;
817  goto error_label;
818  }
819 
820  if(libspoton_create_urls_table(libspotonHandle) != SQLITE_OK)
821  {
822  rerr = LIBSPOTON_ERROR_SQLITE_CREATE_URLS_TABLE;
823  goto error_label;
824  }
825 
826  pthread_mutex_lock(&sqlite_mutex);
827  rv = sqlite3_prepare_v2(libspotonHandle->m_sqliteHandle,
828  sql,
829  (int) strlen(sql),
830  &stmt,
831  0);
832  pthread_mutex_unlock(&sqlite_mutex);
833 
834  if(rv != SQLITE_OK)
835  {
836  rerr = LIBSPOTON_ERROR_SQLITE_PREPARE_V2;
837  goto error_label;
838  }
839 
840  for(i = 1; i <= FIELDS_COUNT; i++)
841  {
842  if(encrypt)
843  {
844  iv = (char *) gcry_calloc(blockLength, sizeof(char));
845 
846  if(iv)
847  {
848 #ifdef LIBSPOTON_OS_OPENBSD
849  gcry_cipher_ctl(cipherCtx, GCRYCTL_RESET, NULL, (size_t) 0);
850 #else
851  gcry_cipher_reset(cipherCtx);
852 #endif
853  gcry_fast_random_poll();
854  gcry_create_nonce(iv, blockLength);
855 
856  if(gcry_cipher_setiv(cipherCtx, iv, blockLength) != 0)
857  {
858  rerr = LIBSPOTON_ERROR_GCRY_CIPHER_SETIV;
859  goto error_label;
860  }
861  }
862  else
863  {
864  rerr = LIBSPOTON_ERROR_GCRY_CALLOC;
865  goto error_label;
866  }
867  }
868 
869  if(i == 1)
870  {
871  buffer = url;
872  length = urlSize;
873  }
874  else if(i == 2)
875  {
876  buffer = title;
877  length = titleSize;
878  }
879  else if(i == 3)
880  {
881  buffer = description;
882  length = descriptionSize;
883  }
884  else
885  {
886  buffer = content;
887  length = contentSize;
888  }
889 
890  if(encrypt)
891  {
892  if(length < blockLength)
893  encodedBufferLength = blockLength;
894  else
895  encodedBufferLength = length;
896 
897  if(SIZE_MAX - encodedBufferLength >= 4)
898  encodedBufferLength += 4; /*
899  ** We'll append the length of the
900  ** original buffer.
901  */
902  else
903  {
904  rerr = LIBSPOTON_ERROR_NUMERIC_ERROR;
905  goto error_label;
906  }
907 
908  if(sizeofchar > 0 && encodedBufferLength <= SIZE_MAX / sizeofchar)
909  encodedBuffer = (char *) calloc
910  (encodedBufferLength, sizeof(char));
911  else
912  encodedBuffer = 0;
913 
914  if(!encodedBuffer)
915  {
916  rerr = LIBSPOTON_ERROR_CALLOC;
917  goto error_label;
918  }
919  else
920  memcpy(encodedBuffer, buffer, length);
921 
922  /*
923  ** Set the last four bytes to the length of the buffer. QDataStream
924  ** objects will retrieve the length of the original message.
925  */
926 
927 #ifdef LIBSPOTON_OS_WINDOWS
928  lengthArray[3] = (char) (length & 0xff);
929  lengthArray[2] = (char) ((length >> 8) & 0xff);
930  lengthArray[1] = (char) ((length >> 16) & 0xff);
931  lengthArray[0] = (char) ((length >> 24) & 0xff);
932  encodedBuffer[encodedBufferLength - 4] = lengthArray[0];
933  encodedBuffer[encodedBufferLength - 3] = lengthArray[1];
934  encodedBuffer[encodedBufferLength - 2] = lengthArray[2];
935  encodedBuffer[encodedBufferLength - 1] = lengthArray[3];
936 #else
937  length = (size_t) htonl((uint32_t) length);
938  memcpy(lengthArray, &length, (size_t) 4);
939  memcpy
940  (&encodedBuffer[encodedBufferLength - 4], lengthArray, (size_t) 4);
941 #endif
942  gcry_fast_random_poll();
943 
944  if(gcry_cipher_encrypt(cipherCtx,
945  encodedBuffer, encodedBufferLength,
946  0, (size_t) 0) == 0)
947  {
948  if(SIZE_MAX - blockLength >= encodedBufferLength)
949  encodedBufferAndIVLength = blockLength + encodedBufferLength;
950  else
951  {
952  rerr = LIBSPOTON_ERROR_NUMERIC_ERROR;
953  goto error_label;
954  }
955 
956  if(sizeofchar > 0 &&
957  encodedBufferAndIVLength <= SIZE_MAX / sizeofchar)
958  encodedBufferAndIV = (char *) calloc
959  (encodedBufferAndIVLength, sizeofchar);
960  else
961  encodedBufferAndIV = 0;
962 
963  if(encodedBufferAndIV)
964  {
965  memcpy(encodedBufferAndIV, iv, blockLength);
966  memcpy(&encodedBufferAndIV[blockLength], encodedBuffer,
967  encodedBufferLength);
968  }
969  else
970  {
971  rerr = LIBSPOTON_ERROR_CALLOC;
972  goto error_label;
973  }
974  }
975  else
976  {
977  rerr = LIBSPOTON_ERROR_GCRY_CIPHER_ENCRYPT;
978  goto error_label;
979  }
980  }
981 
982  if(encrypt)
983  {
984  if(sqlite3_bind_blob(stmt,
985  i,
986  encodedBufferAndIV,
987  (int) encodedBufferAndIVLength,
988  SQLITE_TRANSIENT) != SQLITE_OK)
989  {
990  if(i == 1)
991  rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_URL;
992  else if(i == 2)
993  rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_TITLE;
994  else if(i == 3)
995  rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_DESCRIPTION;
996  else
997  rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_CONTENT;
998 
999  goto error_label;
1000  }
1001  }
1002  else
1003  {
1004  if(sqlite3_bind_blob(stmt,
1005  i,
1006  buffer,
1007  (int) length,
1008  SQLITE_TRANSIENT) != SQLITE_OK)
1009  {
1010  if(i == 1)
1011  rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_URL;
1012  else if(i == 2)
1013  rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_TITLE;
1014  else if(i == 3)
1015  rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_DESCRIPTION;
1016  else
1017  rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_CONTENT;
1018 
1019  goto error_label;
1020  }
1021  }
1022 
1023  free(encodedBuffer);
1024  encodedBuffer = 0;
1025  free(encodedBufferAndIV);
1026  encodedBufferAndIV = 0;
1027  gcry_free(iv);
1028  iv = 0;
1029  }
1030 
1031  if(sqlite3_bind_int(stmt,
1032  5,
1033  encrypt) != SQLITE_OK)
1034  {
1035  rerr = LIBSPOTON_ERROR_SQLITE_BIND_INT_ENCRYPT;
1036  goto error_label;
1037  }
1038 
1039  rv = sqlite3_step(stmt);
1040 
1041  if(!(rv == SQLITE_OK || rv == SQLITE_DONE))
1042  {
1043  rerr = LIBSPOTON_ERROR_SQLITE_STEP;
1044  goto error_label;
1045  }
1046 
1047  sqlite3_finalize(stmt);
1048  stmt = 0;
1049 
1050  error_label:
1051  free(encodedBuffer);
1052  free(encodedBufferAndIV);
1053  gcry_cipher_close(cipherCtx);
1054  gcry_free(iv);
1055  sqlite3_finalize(stmt);
1056  return rerr;
1057  }
1058 
1059  inline pid_t libspoton_registered_kernel_pid
1060  (libspoton_handle_t *libspotonHandle, libspoton_error_t *error)
1061  {
1062  int rv = 0;
1063  const char *sql = "SELECT pid FROM kernel_registration LIMIT 1";
1064  sqlite3_stmt *stmt = 0;
1065  sqlite3_int64 pid = 0;
1066 
1067  if(!libspotonHandle)
1068  goto error_label;
1069  else if(!libspotonHandle->m_sqliteHandle)
1070  goto error_label;
1071 
1072  pthread_mutex_lock(&sqlite_mutex);
1073  rv = sqlite3_prepare_v2(libspotonHandle->m_sqliteHandle, sql, -1, &stmt, 0);
1074  pthread_mutex_unlock(&sqlite_mutex);
1075 
1076  if(rv == SQLITE_OK)
1077  {
1078  if((rv = sqlite3_step(stmt)) == SQLITE_ROW)
1079  pid = sqlite3_column_int64(stmt, 0);
1080  else if(error)
1081  {
1082  if(rv == SQLITE_BUSY || rv == SQLITE_LOCKED)
1083  *error = LIBSPOTON_ERROR_SQLITE_DATABASE_LOCKED;
1084  else if(!(rv == SQLITE_OK || rv == SQLITE_DONE))
1085  *error = LIBSPOTON_ERROR_SQLITE_STEP;
1086  }
1087  }
1088  else if(error)
1089  {
1090  if(rv == SQLITE_BUSY || rv == SQLITE_LOCKED)
1091  *error = LIBSPOTON_ERROR_SQLITE_DATABASE_LOCKED;
1092  else
1093  *error = LIBSPOTON_ERROR_SQLITE_PREPARE_V2;
1094  }
1095 
1096  sqlite3_finalize(stmt);
1097 
1098  error_label:
1099  return (pid_t) pid;
1100  }
1101 
1102  inline void libspoton_close(libspoton_handle_t *libspotonHandle)
1103  {
1104  if(libspotonHandle)
1105  {
1106  gcry_free(libspotonHandle->m_key);
1107  pthread_mutex_lock(&sqlite_mutex);
1108  sqlite3_close(libspotonHandle->m_sqliteHandle);
1109  pthread_mutex_unlock(&sqlite_mutex);
1110  libspotonHandle->m_cipherAlgorithm = 0;
1111  libspotonHandle->m_key = 0;
1112  libspotonHandle->m_keyLength = 0;
1113  libspotonHandle->m_sqliteHandle = 0;
1114  }
1115  }
1116 
1117  inline void libspoton_enable_sqlite_cache(void)
1118  {
1119 #ifndef LIBSPOTON_OS_MAC
1120 #ifndef LIBSPOTON_OS_OS2
1121  sqlite3_enable_shared_cache(1);
1122 #endif
1123 #endif
1124  }
1125 
1126 #ifdef __cplusplus
1127 }
1128 #endif
1129 #endif
libspoton_handle_struct_t
Definition: libspoton.h:99